package com.webchat.pgc.controller.mall;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.domain.vo.request.mall.MallProductStockOperateReqVO;
import com.webchat.domain.vo.request.mall.MallProductStockQueryReqVO;
import com.webchat.pgc.service.mall.MallProductStockService;
import com.webchat.rmi.pgc.mall.MallProductStockClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
public class MallProductStockController implements MallProductStockClient {


    @Autowired
    private MallProductStockService productStockService;


    @Override
    public APIResponseBean<Boolean> reduceStock(@RequestBody MallProductStockOperateReqVO mallProductStockOperateReq) {

        boolean res = productStockService.reduceStock(mallProductStockOperateReq.getSpuId(),
                                                      mallProductStockOperateReq.getSkuId(),
                                                      mallProductStockOperateReq.getNum());
        return APIResponseBeanUtil.success(res);
    }

    @Override
    public APIResponseBean<Boolean> incrementStock(@RequestBody MallProductStockOperateReqVO mallProductStockOperateReq) {

        boolean res = productStockService.incrementStock(mallProductStockOperateReq.getSpuId(),
                                                         mallProductStockOperateReq.getSkuId(),
                                                         mallProductStockOperateReq.getNum());
        return APIResponseBeanUtil.success(res);
    }

    @Override
    public APIResponseBean<Integer> getStock(Long spuId, Long skuId) {

        Integer res = productStockService.getStock(spuId, skuId);
        return APIResponseBeanUtil.success(res);
    }

    @Override
    public APIResponseBean<Map<Long, Integer>> batchGetStock(@RequestBody MallProductStockQueryReqVO mallProductStockQueryReqVO) {

        Map<Long, Integer> res = productStockService.batchGetStock(mallProductStockQueryReqVO.getSpuId(),
                                                                   mallProductStockQueryReqVO.getSkuIdList());
        return APIResponseBeanUtil.success(res);
    }

}
